<?php

declare (strict_types=1);
namespace cms\office\model;

use Carbon\Carbon;
use Hyperf\Database\Model\Relations\HasOne;

/**
 * @property int $id
 * @property int $app_id 应用标识
 * @property int $enterprise_id 企业标识
 * @property string $name 名称标识
 * @property Carbon $start_at 开始时间
 * @property Carbon $finish_at 结束时间
 * @property int $amount 项目总额
 * @property int $status 状态,1|正常、2|禁用
 * @property Carbon $created_at 创建时间
 * @property Carbon $updated_at 更新时间
 * @property Carbon $deleted_at 删除时间
 */
class ProjectModel extends CommonModel
{
    /**
     * @var string
     */
    protected $table = 'office_project';

    protected $appends = ['disburse_amount', 'receipt_amount'];

    public function getDisburseAmountAttribute()
    {
        $project_id = $this->getAttribute('id');
        if ($project_id) {
            return DisburseModel::where('project_id', $project_id)->sum('amount') / 100;
        }
        return 0;
    }
    public function getReceiptAmountAttribute()
    {
        $project_id = $this->getAttribute('id');
        if ($project_id) {
            return ReceiptModel::where('project_id', $project_id)->sum('amount') / 100;
        }
        return 0;
    }

    /**
     * 关联应用
     * @return HasOne
     */
    public function app(): HasOne
    {
        return $this->hasOne(AppModel::class, 'id', 'app_id');
    }

    /**
     * 关联企业
     * @return HasOne
     */
    public function enterprise(): HasOne
    {
        return $this->hasOne(EnterpriseModel::class, 'id', 'enterprise_id');
    }
}